<?php
class Media {
    function convert_media($filename) {

        $rootpath= dirname(__FILE__)."/../../../media/movie";
        $inputpath=$rootpath."/temp";
        $outputpath=$rootpath."/flv";
        $width=470;
        $height=470;
        $bitrate= "32k";
        $samplingrate = 22050;

// which shows FFMPEG folder exist on the root.
// Set Media Size that is width and hieght
        $size = $width."x".$height;
// remove origination extension from file adn add .flv extension, becuase we must give output file name to ffmpeg command.
        $outfile = $filename;
        $out=explode(".",$outfile);


// Media Size
//$size = Width & "x" & Height;

// remove origination extenstion from file and add .flv extension , becuase we must give output filename to ffmpeg command.

        $outfile = $out[0].".flv";
// Use exec command to access command prompt to execute the following FFMPEG Command and convert video to flv format.
    //ffmpeg -i $input/$name -ar 22050 -ab 32k -f flv -y $output/$nnm[0].flv
        //ffmpeg -i $input/$name -ar 22050 -ab 32k -g 10 -f flv -b 700k -s 320x240 -y $output/$nnm[0].flv
              $ffmpegcmd1 = "ffmpeg -i ".$inputpath."/".$filename. " -ar " .$samplingrate." -ab ".$bitrate." -g 10 -f flv -b 700k -s ".$size." ".$outputpath."/".$outfile;

        //$ffmpegcmd1 = "ffmpeg -i ".$inputpath."/".$filename. " -ar " .$samplingrate." -ab ".$bitrate." -f flv -s ".$size." ".$outputpath."/".$outfile;
        //ffmpeg -i video.avi -ar 22050 -ab 32k -f flv -b 700k -s 400x300 -y video.flv
        //$ffmpegcmd1 = "ffmpeg -i ".$inputpath."/".$filename. " -b 500 -r 25 -s 320×240 -hq -deinterlace -ab 56 -ar 22050 -ac 1 ".$outputpath."/".$outfile."";


        $ret = exec($ffmpegcmd1);
        if(!file_exists($outputpath."/".$outfile)){
            //unlink($inputpath."/".$filename);
            die($ffmpegcmd1);
            return false;
        }
        else {
        chmod($outputpath."/".$outfile, 777);
        unlink($inputpath."/".$filename);
        $this->grab_image($outfile, $rootpath."/flv", $rootpath."/screen",  $image_format, 100, 100);
        }
        return true;
    }
    function grab_image($filename, $inputpath,$outputpath,  $image_format, $width, $height) {
// root directory path
// Media Size
        $size = $width."x".$height;

// I am using static image, you can dynamic it with your own choice.
        $out=explode(".",$filename);

        $outfile = $out[0].".jpg";

        $ffmpegcmd1 = "ffmpeg -i ".$inputpath."/".$filename." -ss 00:00:10 -an -vframes 1 -vcodec mjpeg -f rawvideo -s ".$size. " ". $outputpath."/".$outfile;
// -ss 00:00:10 -an -vframes 1 -vcodec mjpeg -f rawvideo -s 100x100 /home/insane/NetBeansProjects/oriental/protected/backend/components/../../../media/movie/screen/PinkElephantsCrew08.jpg

        $ret = exec($ffmpegcmd1);
        if(file_exists($outputpath."/".$outfile)) chmod($outputpath."/".$outfile, 777);
        else die($ffmpegcmd1);
// Execute this command using exec command or any other tool to grab image from converted flv file.
        return $ffmpegcmd1;
        //яебусобак
    }
}
?>